package com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiData.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiData.entity.SamPoiData;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.zcpt.sys.service.SysAreaService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;

@Component
public class SamPoiExcelVerifyHandler implements IExcelVerifyHandler<SamPoiData> {

    @Autowired
    SysAreaService sysAreaService;

    @SneakyThrows
    @Override
    public ExcelVerifyHandlerResult verifyHandler(SamPoiData samPoiData) {
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");

        if (!PlatformObjectUtils.isEmpty(samPoiData.getProvince()) && !PlatformObjectUtils.isEmpty(samPoiData.getCity())  && !PlatformObjectUtils.isEmpty(samPoiData.getArea())) {
            String divisionIds = null;
            try {
                divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(samPoiData.getProvince(),"utf-8"),URLEncoder.encode(samPoiData.getCity(),"utf-8"),URLEncoder.encode(samPoiData.getArea(),"utf-8"));
            } catch (Exception e){

            }
            if (PlatformObjectUtils.isEmpty(divisionIds)) {
                result.setSuccess(false);
                message.append("省市区县名称 不符合规范！");
            }
        }else{
            if (PlatformObjectUtils.isEmpty(samPoiData.getProvince())) {
                result.setSuccess(false);
                message.append("省 不能为空！");
            }
            if (PlatformObjectUtils.isEmpty(samPoiData.getCity())) {
                result.setSuccess(false);
                message.append("市 不能为空！");
            }
            if (PlatformObjectUtils.isEmpty(samPoiData.getArea())) {
                result.setSuccess(false);
                message.append("区(县) 不能为空！");
            }
        }

        if (PlatformObjectUtils.isEmpty(samPoiData.getAddress())) {
            result.setSuccess(false);
            message.append("详细地址 不能为空！");
        }
        if (PlatformObjectUtils.isEmpty(samPoiData.getLongitude())) {
            result.setSuccess(false);
            message.append("经度 不能为空！");
        }
        if (PlatformObjectUtils.isEmpty(samPoiData.getLatitude())) {
            result.setSuccess(false);
            message.append("纬度 不能为空！");
        }
        if (PlatformObjectUtils.isEmpty(samPoiData.getSamplingPointName())) {
            result.setSuccess(false);
            message.append("抽样点名称 不能为空！");
        }
        result.setMsg(message.toString());
        return result;
    }
}
